//
//  BKLHealth_MCFamousDoctorTableCell.m
//  BKLMedicalCare
//
//  Created by ma c on 2017/6/15.
//  Copyright © 2017年 q mac. All rights reserved.
//

#import "BKLHealth_MCTableCell.h"
#import "BKLHealth_MCCommon.h"
#import "BKLHealth_MCCellModel.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
@implementation BKLHealth_MCTableCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (void)fillData:(id)data {}
+ (CGFloat)computeHeight:(id)data {return 0;}

@end

#pragma mark - 所有科室cell
#pragma mark -

// table cell 上 collection view 的 item (er zi)
@interface BKLHealth_MCFDDepartmentItem : UICollectionViewCell

@end
@implementation BKLHealth_MCFDDepartmentItem
{
    UIImageView *_imageView;
    UILabel *_label;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _imageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_imageView];
    
    _label = [[UILabel alloc] init];
    _label.textColor = kBklHealth_bklBlack;
    _label.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_label];
    
    [self makeConstraints];
}

- (void)makeConstraints {
    [_imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView.mas_top).offset(16);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
        make.height.with.mas_equalTo(40);
    }];
    
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageView.mas_bottom).offset(8);
        make.centerX.mas_equalTo(self.contentView.mas_centerX);
    }];
}

- (void)fillData:(id)data {
    if (!data) {
        return;
    }
    NSString *imageString = data[@"imageString"];
    NSString *content = data[@"content"];
    
    _imageView.image = [UIImage imageNamed:imageString];
    _label.text = content;
}

@end

// table cell
@interface BKLHealth_MCFDDepartmentCell ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>

@end

@implementation BKLHealth_MCFDDepartmentCell
{
    UICollectionView *_collectionView;
    UIPageControl *_pageControl;
    NSArray *_sectionArray;
}

NSString *const departmentItemIndetifier = @"departmentItemIndetifier";

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    
    _sectionArray = [NSArray array];
    
    _collectionView = ({
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.pagingEnabled = YES;
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[BKLHealth_MCFDDepartmentItem class] forCellWithReuseIdentifier:departmentItemIndetifier];
        collectionView;
    });
    [self.contentView addSubview:_collectionView];
    
    _pageControl = ({
        UIPageControl *pageControl = [[UIPageControl alloc] init];
        pageControl.currentPageIndicatorTintColor = kBklHealth_bklYellow;
        pageControl.numberOfPages = 2;
        pageControl.userInteractionEnabled = NO;
        pageControl;
    });
    
    [self.contentView addSubview:_pageControl];
    
    [self makeConstraints];
}

- (void)makeConstraints {
    [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(_pageControl.mas_top);
    }];
    
    [_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.contentView);
        make.bottom.mas_equalTo(self.contentView);
        make.height.mas_equalTo(24);
    }];
}

- (void)fillData:(id)data {
    if (!data) {
        return;
    }
    _sectionArray = data;
}

+ (CGFloat)computeHeight:(id)data {
    return 190;
}

#pragma mark collection delelgate

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 8;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BKLHealth_MCFDDepartmentItem *item = [collectionView dequeueReusableCellWithReuseIdentifier:departmentItemIndetifier forIndexPath:indexPath];
    return item;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *cellArr = _sectionArray[indexPath.section];
    NSDictionary *cm = cellArr[indexPath.row];
    BKLHealth_MCFDDepartmentItem *item = (BKLHealth_MCFDDepartmentItem *)cell;
    [item fillData:cm];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (collectionView.frame.size.width - 19) / 4.0;
    CGFloat height = collectionView.frame.size.height / 2.0;
    return CGSizeMake(width, height);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 9.5, 0, 9.5);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (self.delegate && [self.delegate respondsToSelector:@selector(departmentCell:didSelectedItem:)]) {
        [self.delegate departmentCell:self didSelectedItem:indexPath];
    }
}

// scroll view delegate

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSInteger currentIndex = [self currentIndex];
    _pageControl.currentPage = currentIndex;
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    NSInteger currentIndex = [self currentIndex];
    _pageControl.currentPage = currentIndex;
}

- (NSInteger)currentIndex {
    // 只支持横向
    NSInteger index = 0;
    index = (_collectionView.contentOffset.x + _collectionView.frame.size.width * 0.5) / _collectionView.frame.size.width;
    return index;
}
@end


#pragma mark - 预约体检cell
#pragma mark -

@implementation BKLHealth_MCFDAppointmentOfMedicalCell
{
    UIImageView *_titleImgv;
    UILabel *_titleLab;
    UIImageView *_arrowImgv;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _titleImgv = [[UIImageView alloc] init];
    _titleImgv.layer.masksToBounds = YES;
    _titleImgv.layer.cornerRadius = 22;
    [self.contentView addSubview:_titleImgv];
    
    _titleLab = [[UILabel alloc] init];
    _titleLab.textColor = kBklHealth_bklBlack;
    _titleLab.font = [UIFont systemFontOfSize:16];
    [self.contentView addSubview:_titleLab];
    
    _arrowImgv = [[UIImageView alloc] init];
    [self.contentView addSubview:_arrowImgv];
    
    [self makeConstraints];
}

- (void)makeConstraints {
    [_titleImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.left.mas_equalTo(self.contentView.mas_left).mas_offset(12);
        make.height.with.mas_equalTo(44);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleImgv.mas_right).mas_offset(14);
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
    }];
    
    [_arrowImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView.mas_centerY);
        make.right.mas_equalTo(self.contentView.mas_right).offset(-10);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(14);
    }];
}

- (void)fillData:(id)data {
    if (!data) {
        return;
    }
    _titleImgv.image = [UIImage imageNamed:data[@"titleImageString"]];
    _titleLab.text = data[@"title"];
    _arrowImgv.image = [UIImage imageNamed:data[@"arrowImageString"]];
}

+ (CGFloat)computeHeight:(id)data {
    return 72;
}
@end


#pragma mark - 文章分页 table cell

@implementation BKLHealth_MCArticleCell
{
    UIImageView *_mainImgv;
    UILabel *_titleLab;
    UILabel *_deputyLab;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
    _mainImgv = [[UIImageView alloc] init];
    [self.contentView addSubview:_mainImgv];
    
    _titleLab = [[UILabel alloc] init];
    _titleLab.textColor = kBklHealth_bklBlack;
    _titleLab.font = [UIFont systemFontOfSize:16];
    _titleLab.numberOfLines = 2;
    [self.contentView addSubview:_titleLab];
    
    _deputyLab = [[UILabel alloc] init];
    _deputyLab.textColor = kBklHealth_HexColor(0x919090);
    _deputyLab.font = [UIFont systemFontOfSize:12];
    [self.contentView addSubview:_deputyLab];
    
    [self makeConstraints];
}

- (void)makeConstraints {
    
    [_mainImgv mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.contentView).offset(12);
        make.left.mas_equalTo(self.contentView).offset(12);
        make.height.mas_equalTo(88);
        make.width.mas_equalTo(120);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_mainImgv);
        make.left.mas_equalTo(_mainImgv.mas_right).offset(14);
        make.right.mas_equalTo(self.contentView).offset(-10);
    }];
    
    [_deputyLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_titleLab);
        make.bottom.mas_equalTo(_mainImgv);
    }];
}

- (void)fillData:(id)data {
    if (!data) {
        return;
    }
    
    NSString *imageUrl = data[@"imageUrl"];
    NSString *title = data[@"title"];
    NSString *deputyText = data[@"deputyText"];
    
    [_mainImgv sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@"产科"]];
    _titleLab.text = title;
    _deputyLab.text = deputyText;
    
}

+ (CGFloat)computeHeight:(id)data {
    return 110;
}

@end

